* Input cost shares from WIOD IO table (US 2000)
* Downloaded from: http://www.wiod.org/database/niots16


global tmp "/tmp"
cd /Users/fcoell/Dropbox/PATSTAT/DATA/
cd /users/andreas/dropbox/work/patstat/data

// Import IO table
import excel IO_tables/USA_niot_nov16.xlsx, sheet("National IO-tables") firstrow case(lower) clear
keep if year == 2000
drop cons_* gfcf inven exp exp go year description origin
drop if _n > 113
rename code isic4_code_orig
foreach v of varlist a* b c* d* e* f g* h* i j* k* l* m* n o* p* q r* t u {
    destring `v', replace
}

// Sum domestic and imported input costs
gen id = 1
preserve
drop if isic4_code_orig == "II_fob"
collapse (sum) a* b c* d* e* f g* h* i j* k* l* m* n o* p* q r* t u, by(isic4_code_orig id)
save $tmp/inputs_cost, replace
restore

// Total input cost
keep if isic4_code_orig == "II_fob"
foreach v of varlist a* b c* d* e* f g* h* i j* k* l* m* n o* p* q r* t u {
    rename `v' tot_`v'
}
drop isic4_code_orig
save $tmp/tot_cost, replace

// Merge totals
use $tmp/inputs_cost, clear
merge m:1 id using $tmp/tot_cost, assert(match)

// Calculate cost shares for each industry
foreach v of varlist a* b c* d* e* f g* h* i j* k* l* m* n o* p* q r* t u {
    gen w_`v' = `v' / tot_`v'
    replace w_`v' = 0  if w_`v' == .
}
save $tmp/cost_shares_isic4, replace

// Check shares sum to 1
collapse (sum) w_*
sum


// Get all Isic codes when  WIOD output industries are grouped together
use $tmp/inputs_cost, clear
keep isic4_code_orig
clonevar isic = isic4_code_orig
split isic, p("-" "_")
gen isic3 = "C11" if isic == "C10-C12"
replace isic3 = "C14" if isic == "C13-C15"
replace isic3 = "E38" if isic == "E37-E39"
egen id = group(isic4_code_orig)
drop isic
reshape long isic, i(id) j(num)
drop if isic == ""
egen n_codes = max(num), by(id)
keep isic4_code_orig isic n_codes
save $tmp/isic_ind, replace


// Split cost shares equally when input industries are grouped together
/* When cost shares of n>1 input industry are grouped together, assign 1/n to each input industry */
use $tmp/cost_shares_isic4, clear
keep isic4_code_orig w_*
merge 1:m isic4_code_orig using $tmp/isic_ind, keepusing(isic n_codes) assert(match)
foreach v of varlist w_*{
    replace `v' = `v' / n_codes
}
drop _merge
save $tmp/cost_shares_isic_all, replace

// Check shares sum to 1
collapse (sum) w_*
sum

// Isic4 to Nace2
use $tmp/cost_shares_isic_all, clear
keep isic
// Extract 2-digit numeric part of Isic code
gen isic4_2d = regexs(0) if(regexm(isic, "[0-9][0-9]"))
gen isic4_1d = regexs(0) if(regexm(isic, "[A-Z]"))
clonevar isic4 = isic4_2d
replace isic4 = isic4_1d if isic4_2d == ""
// Convert to Nace2 - nace and isic should be identical - check
rename isic4  isic4code
merge 1:m isic4code using Correspondence/ISIC4_NACE2/ISIC4_NACE2, ///
    keep(match master) keepusing(nace2code)
assert isic4code == nace2code
drop _merge
drop isic4code
save $tmp/nace2_ind, replace


// Reshape output industries
use $tmp/cost_shares_isic_all, clear
merge 1:1 isic using $tmp/nace2_ind, assert(match)
order nace2code isic4_1d isic4_2d
drop isic4_code_orig isic n_codes _merge
rename nace2code inp_nace2
drop isic4_?d
reshape long w_ , i(inp_nace2) j(outind) string
rename w_ w
gen isic4_code_orig = upper(outind)
// Get all Isic codes when WIOD output industries are grouped together
replace isic4_code_orig = "C10-C12" if isic4_code_orig == "C10C12"
replace isic4_code_orig = "C13-C15" if isic4_code_orig == "C13C15"
replace isic4_code_orig = "E37-E39" if isic4_code_orig == "E37E39"
joinby isic4_code_orig using $tmp/isic_ind, unmatched(master)
drop _merge
gen out_isic4_2d = regexs(0) if(regexm(isic, "[0-9][0-9]"))
gen out_isic4_1d = regexs(0) if(regexm(isic, "[A-Z]"))
clonevar out_nace2 = out_isic4_2d
replace out_nace2 = out_isic4_1d if out_isic4_2d == ""
drop isic4_code_orig isic out_isic4_2d out_isic4_1d outind
sort out_nace2 inp_nace2
order out_nace2 inp_nace2 w n_codes

save input_cost_shares, replace

// Check shares sum to 1
collapse (sum) w, by(out_nace2)
sum
sum if out_nace2 != "U"

****************************
* Calculate input tariffs
****************************

* Tariff for home country
use tariffreshape, clear
contract appln
drop _freq
save /tmp/cty, replace

use input_cost_shares, clear

* Mining and quarrying: Use number 06 instead of letter "B"
replace inp_nace2="06" if inp_nace=="B"
replace out_nace2="06" if out_nace=="B"
ren inp_nace2 nace2_1
destring nace2, replace force
replace nace2=99 if nace2==.
collapse (sum) w, by(out_nace nace2)
cross using /tmp/cty
merge m:1 nace2 appln using tariffreshape, keep(match master)
forvalues y = 1992/2009 {
	replace tariff`y' = 0 if nace2>=36  // Nace sectors 36 and up are non-traded
	replace tariff`y' = 0 if nace2==12  // Tariff missing for the US++ (tobacco)
	gen inptariff`y' = w*tariff`y'  
}
collapse (sum) w inptariff*, by(out appl)
drop if out_nace=="U"
save input_tariff, replace

